/*
   The contents of this file are subject to the Mozilla Public License
   Version 1.1 (the "License"); you may not use this file except in
   compliance with the License. You may obtain a copy of the License at
   http://www.mozilla.org/MPL/

   Software distributed under the License is distributed on an "AS IS"
   basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
   License for the specific language governing rights and limitations
   under the License.

   The Original Code is: www.iwobanas.com code samples.

   The Initial Developer of the Original Code is Iwo Banas.
   Portions created by the Initial Developer are Copyright (C) 2009
   the Initial Developer. All Rights Reserved.

   Contributor(s):
 */
package com.iwobanas.controls.dataGridClasses.filters
{
	import com.barcap.controls.BCDataGrid;
	import com.barcap.customui.utils.BUtils;
	import com.iwobanas.controls.MDataGrid;
	import com.iwobanas.controls.dataGridClasses.MDataGridColumn;
	import com.iwobanas.controls.dataGridClasses.MDataGridEvent;
	import com.iwobanas.utils.WildcardUtils;
	
	import mx.collections.ArrayCollection;
	import mx.collections.Sort;

	/**
	 * The WildcardFilter class defines MDataGrid column filter
	 * using text wildcard to filter items.
	 *
	 * This filter matches item labels to a given wildcard
	 * and if they don't match eliminate items from data provider.
	 *
	 * If wildcard is not set or is empty string filter is regarded inactive.
	 */
	public class WildcardFilter extends ColumnFilterBase
	{
		public var strict:Boolean;
		[Bindable]
		public var labels:ArrayCollection;

		/**
		 * Constructor.
		 */
		public function WildcardFilter(column:MDataGridColumn, strict:Boolean = false)
		{
			super(column);
			//added by bok
			this.strict = strict;
			dataGrid.addEventListener(MDataGridEvent.ORIGINAL_COLLECTION_CHANGE, originalCollectionChandeHandler, false, 0, true);
		}

		/**
		 * Wildcard to be matched against item labels.
		 * "*" is interpreted as any character sequence.
		 * "?" is interpreted as any single character.
		 * This wildcard is case insensitive.
		 */
		[Bindable]
		public function get wildcard():String
		{
			return _wildcard;
		}

		/**
		 * @private
		 */
		public function set wildcard(value:String):void
		{
			_wildcard = value;
			try
			{
				regExp = WildcardUtils.wildcardToRegExp(value);
				commitFilterChange();
			}
			catch (e:Error)
			{
				BUtils.showMessage("Invalid Filter",BUtils.ERROR_MESSAGE_TYPE);
			}
		}

		public function set initializeWildcard(value:String):void
		{
			_wildcard = value;
			regExp = WildcardUtils.wildcardToRegExp(value);
		}
		/**
		 * @private
		 * Storage variable for wildcard property.
		 */
		protected var _wildcard:String;

		/**
		 * Current wildcard converted to regular expression.
		 */
		[Bindable]
		public var regExp:RegExp;

		/**
		 * Flag indicating wether this filter is active
		 * i.e may eliminate some items from MDataGrid data provider.
		 */
		override public function get isActive():Boolean
		{
			return (_wildcard != null && _wildcard != "");
		}

		protected function updateLabels():void
		{
			//TODO: save filter selection when data are updated
			var nl:ArrayCollection = new ArrayCollection();
			var gridDataProvider:*;
			if (dataGrid is BCDataGrid)
				gridDataProvider = BCDataGrid(dataGrid).originalDataProvider;
			else
				gridDataProvider = MDataGrid(dataGrid).originalCollection;
			for each (var item:Object in gridDataProvider.source)
			{
				var label:String = column.itemToLabel(item);
				if (label && !nl.contains(label))
				{
					nl.addItem(label);
				}
			}
			nl.sort = new Sort();
			nl.refresh();
			labels = nl;
		/* if(!initialFilter){
		   deselectAll();
		   selectAll();
		   initialFilter=false;
		 } */
		}

		/**
		 * MDataGrid original collection change event handler.
		 */
		protected function originalCollectionChandeHandler(event:Event):void
		{
			updateLabels();
		}

		/**
		 * Test if given MDataGrid item should remain in MDataGrid data provider.
		 */
		override public function filterFunction(obj:Object):Boolean
		{
			if (!regExp)
			{
				return true;
			}
			var d:String = column.itemToLabel(obj);
			if (d == " ")
			{
				d = "null";
				return regExp.test(d);
			}
			else
			{
				if (strict)
				{
					var b:Boolean = (d == regExp.source);
					return b;
				}
				return regExp.test(column.itemToLabel(obj));
			}
		}

	}
}